<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/admin/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/css/public.css" media="all">
    <style>
        .ui-input-class{
            width: 60%;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">sim卡号</label>
                            <div class="layui-input-inline">
                                <input type="text" id="simCardd" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>

            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="employeeTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">领取</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">安装</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="dd">删除</a>
        </script>

    </div>
</div>
<div class="layui-form layuimini-form" id="SimAdd" style="display: none;">
    <div class="layui-form-item" style="margin-top: 20px;">
        <label class="layui-form-label required">Sim卡号</label>
        <div class="layui-input-block">
            <input type="text" name="simCard" id="simCard" lay-verify="required" lay-reqtext="Sim卡号不能为空" placeholder="请输入Sim卡号" value=""
                   class="layui-input ui-input-class">
            <tip>填写好Sim卡号</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">Sim编号</label>
        <div class="layui-input-block">
            <input type="text" name="simNo" id="simNo" lay-verify="required" lay-reqtext="Sim编号不能为空" placeholder="请输入Sim编号" value=""
                   class="layui-input ui-input-class">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">到货时间</label>
        <div class="layui-input-block">
            <input id="toTime" type="text" name="toTime"  lay-verify="required" lay-reqtext="到货时间不可为空" placeholder="点击选择日期" value=""
                   class="layui-input ui-input-class">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">领取人</label>
        <div class="layui-input-block" style="width: 20%;">
            <select class="ui-input-class" id="employeeId"  name="employeeId"  lay-verify="required">

            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">车牌号</label>
        <div class="layui-input-block" style="width: 20%;">
            <select class="ui-input-class" id="carRegisterId"  name="carRegId"  lay-verify="required">

            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="/static/admin/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        var laydate = layui.laydate;
        //执行一个laydate
        laydate.render({
            elem: '#toTime' //指定元素
        });
        table.render({
            elem: '#currentTableId',
            url: '/admin/carrier/queryAllSim',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                {type: "checkbox", width: 50},
                {field: 'simId', title: 'ID',hide:true},
                    {field: 'simCard', title: 'sim卡号'},
                    {field: 'toTime', title: '到货时间'},
                    {field: 'getTime', title: '领取时间'},
                    {field: 'status', title: '状态',templet:function(res){
                            if(res.status==0){
                                return "未领取"
                            }else if(res.status==1){
                                return "已领取"
                            }else{
                                return "已安装"
                            }
                        }},
                    {field: 'carNumber', title: '车牌号'},
                    {field: 'employeeName', title: '领取人'},
                    {field: 'createTime', title: '创建时间'},
                    {title: '操作', minWidth: 200, toolbar: '#currentTableBar', align: "center"}
            ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);


            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    simCard: $("#simCardd").val()
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(employeeTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({

                    title: '添加SIM',
                    type: 1,
                    area: ['70%', '80%'],
                    content:$('#SimAdd'),
                    success:function(res){
                        $("#simCard").val("");
                        $("#simNo").val("");
                        $("#toTime").val("");
                        $('#employeeId').html("");
                        $('#carRegisterId').html("");
                        //新增页面员工下拉菜单
                        $.ajax({
                            url:'/admin/carrier/queryAllEmployee',
                            type:'get',
                            dataType:'json',
                            success:function(data){
                                for(var i = 0; i < data.length; i++){
                                    $('#employeeId').append(new Option(data[i].employeeName,data[i].employeeId));
                                }
                                layui.form.render("select");
                            }
                        })
                        //新增页面车牌号下拉菜单
                        $.ajax({
                            url:'/admin/carrier/queryCarNumber',
                            type:'get',
                            dataType:'json',
                            success:function(data){
                                for(var i = 0; i < data.length; i++){
                                    $('#carRegisterId').append(new Option(data[i].carNumber,data[i].carRegisterId));
                                }
                                layui.form.render("select");
                            }
                        })
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(employeeTableFilter)', function (obj) {
            console.log(obj)
        });
        //监听表单提交
        form.on('submit(saveBtn)', function(data){
            $.ajax({
                url:'/admin/carrier/addSim',
                type:'post',
                data:data.field,
                dataType:'json',
                success:function (res) {



                    layer.closeAll('page'); //关闭所有页面层
                    layer.msg(res.message)
                    //刷新表格
                    table.reload('currentTableId', {
                        url: '/admin/carrier/queryAllSim'

                    });


                }

            })

            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        table.on('tool(employeeTableFilter)', function (obj) {

            var data = obj.data;

            //恢复sim卡查看sim卡的状态是否为已安装
            var status = data.status;

            if (obj.event === 'edit') {


                layer.confirm('是否要更改sim的状态？', function (index) {

                    if (status == 1){
                        layer.msg("此sim已领取")
                    }else if(status == 2){
                        layer.msg("此sim已安装")
                    } else{
                        //修改sim卡的状态
                        $.ajax({
                            url:'/admin/carrier/updateStatusAndGetTime',
                            type:'post',
                            data:{
                                simId:data.simId,
                                status:1,


                            },
                            dataType:'json',
                            success:function (res) {
                                layer.msg(res.message)
                                //刷新表格
                                table.reload('currentTableId', {
                                    url: '/admin/carrier/queryAllSim'

                                });
                            }
                        })
                    }

                });

            } else if (obj.event === 'delete') {

                layer.confirm('是否要更改sim的状态？', function (index) {

                    if (status == 2){
                        layer.msg("此sim已是安装状态")
                    }else if(status == 0){
                        layer.msg("此sim要先领取才能安装")
                    }else{
                        //修改sim卡的状态
                        $.ajax({
                            url:'/admin/carrier/updateStatus',
                            type:'post',
                            data:{

                                simId:data.simId,
                                status:2

                            },
                            dataType:'json',
                            success:function (res) {
                                layer.msg(res.message)

                                //刷新表格
                                table.reload('currentTableId', {
                                    url: '/admin/carrier/queryAllSim'

                                });

                            }
                        })
                    }

                });
            }else if(obj.event === 'dd'){
                layer.confirm('是否要删除sim卡信息？', function (index) {

                    //删除sim卡信息
                $.ajax({
                    url:'/admin/carrier/deleteSim',
                    type:'post',
                    data:{

                        simId:data.simId


                    },
                    dataType:'json',
                    success:function (res) {
                        layer.msg(res.message)

                        //刷新表格
                        table.reload('currentTableId', {
                            url: '/admin/carrier/queryAllSim'

                        });

                    }
                })
                })
            }
        });


    });
</script>

</body>
</html>
